package com.iot.toolbox.controller;

import com.iot.toolbox.service.ChecksumService;
import com.iot.toolbox.service.DataConversionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequiredArgsConstructor
public class ToolboxController {

    private final DataConversionService dataConversionService;
    private final ChecksumService checksumService;

    @GetMapping("/")
    public String index(Model model) {
        return "index";
    }

    @PostMapping("/api/convert")
    @ResponseBody
    public Map<String, Object> convert(@RequestParam String operation,
                                      @RequestParam String input,
                                      @RequestParam(required = false) String param1,
                                      @RequestParam(required = false) String param2) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String result = "";
            
            // 数据清洗 - Base64操作除外
            boolean isBase64Operation = operation.equals("encodeBase64") || operation.equals("decodeBase64");
            String cleanedInput = dataConversionService.cleanData(input, isBase64Operation);
            
            switch (operation) {
                case "hexToAscii":
                    result = dataConversionService.hexToAscii(cleanedInput);
                    break;
                case "asciiToHex":
                    result = dataConversionService.asciiToHex(cleanedInput);
                    break;
                case "toUpperCase":
                    result = dataConversionService.toUpperCase(cleanedInput);
                    break;
                case "toLowerCase":
                    result = dataConversionService.toLowerCase(cleanedInput);
                    break;
                case "reverse":
                    result = dataConversionService.reverse(cleanedInput);
                    break;
                case "addSpaces":
                    result = dataConversionService.addSpaces(cleanedInput);
                    break;
                case "removeSpaces":
                    result = dataConversionService.removeSpaces(input); // 去空格不需要清洗输入
                    break;
                case "add33":
                    result = dataConversionService.add33(cleanedInput);
                    break;
                case "subtract33":
                    result = dataConversionService.subtract33(cleanedInput);
                    break;
                case "hexToBinary":
                    result = dataConversionService.hexToBinary(cleanedInput);
                    break;
                case "binaryToHex":
                    result = dataConversionService.binaryToHex(cleanedInput);
                    break;
                case "encodeBase64":
                    result = dataConversionService.encodeBase64(cleanedInput); // Base64使用16进制方式
                    break;
                case "decodeBase64":
                    result = dataConversionService.decodeBase64(input); // Base64解码不清洗
                    break;
                case "getCurrentTimestamp":
                    result = dataConversionService.getCurrentTimestamp();
                    break;
                case "timestampToDateTime":
                    result = dataConversionService.timestampToDateTime(cleanedInput);
                    break;
                case "dateTimeToTimestamp":
                    result = dataConversionService.dateTimeToTimestamp(input); // 日期时间格式需要保留空格
                    break;
                default:
                    throw new IllegalArgumentException("不支持的操作: " + operation);
            }
            
            response.put("success", true);
            response.put("result", result);
            response.put("charCount", result.length());
            
        } catch (Exception e) {
            response.put("success", false);
            response.put("error", e.getMessage());
        }
        
        return response;
    }

    @PostMapping("/api/batch")
    @ResponseBody
    public Map<String, Object> batchProcess(@RequestParam String operations,
                                           @RequestParam String input) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String[] ops = operations.split(",");
            String result = input;
            
            for (String op : ops) {
                op = op.trim();
                switch (op) {
                    case "hexToAscii":
                        result = dataConversionService.hexToAscii(result);
                        break;
                    case "asciiToHex":
                        result = dataConversionService.asciiToHex(result);
                        break;
                    case "toUpperCase":
                        result = dataConversionService.toUpperCase(result);
                        break;
                    case "toLowerCase":
                        result = dataConversionService.toLowerCase(result);
                        break;
                    case "reverse":
                        result = dataConversionService.reverse(result);
                        break;
                    case "add33":
                        result = dataConversionService.add33(result);
                        break;
                    case "subtract33":
                        result = dataConversionService.subtract33(result);
                        break;
                    default:
                        // 跳过不支持的操作
                        break;
                }
            }
            
            response.put("success", true);
            response.put("result", result);
            response.put("charCount", result.length());
            
        } catch (Exception e) {
            response.put("success", false);
            response.put("error", e.getMessage());
        }
        
        return response;
    }
}
